package elephant.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author icecooly
 *
 */
public class IOUtil {
	private static Logger logger=LoggerFactory.getLogger(IOUtil.class);
	//default 4096 char buffer
	private static final int DEFAULT_BUFFER_SIZE=4096;
	private IOUtil(){}
	/**
	 */
	public static void closeQuietly(OutputStream output) {
        try {
            if (output != null) {output.flush();output.close();}
        } catch (IOException ioe) {logger.error(ioe.getMessage(),ioe);}
    }
	//
	/**
	 */
	public static void closeQuietly(InputStream input) {
        try {
            if (input != null) {input.close();}
        } catch (IOException ioe) {logger.error(ioe.getMessage(),ioe);}
    }
	//
	/**
	 */
	public static long copy(InputStream input, OutputStream output) 
	throws IOException {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        long count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }
	/**
	 * 
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public static String getContent(InputStream in) throws IOException{
		try(InputStreamReader isr=new InputStreamReader(in);
				BufferedReader br=new BufferedReader(isr);){
			StringBuilder sb=new StringBuilder();
			String s=null;
			while((s=br.readLine())!=null){
				sb.append(s).append("\n");
			}
			return sb.toString();
		}
	}
	//
	public static byte[] toByteArray(InputStream input) throws IOException {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		byte[] buf = new byte[1024];
		for (int n = input.read(buf); n != -1; n = input.read(buf)) {
			os.write(buf, 0, n);
		}
		return os.toByteArray();
	}
	//
	public static String toString(InputStream input) throws IOException {
		StringWriter sw = new StringWriter();
		copy(input, sw);
		return sw.toString();
	}
	//
	public static void copy(InputStream input, Writer output)
			throws IOException {
		InputStreamReader in = new InputStreamReader(input); // NOSONAR
		copy(in, output);
	}
	//
	public static int copy(Reader input, Writer output) throws IOException {
		long count = copyLarge(input, output);
		if (count > Integer.MAX_VALUE) {
			return -1;
		}
		return (int) count;
	}
	//
	public static long copyLarge(Reader input, Writer output)
			throws IOException {
		char[] buffer = new char[1024 * 4];
		long count = 0;
		int n = 0;
		while (-1 != (n = input.read(buffer))) {
			output.write(buffer, 0, n);
			count += n;
		}
		return count;
	}
	//
	public static boolean delDirectory(File directory) {
		if(!directory.exists()){
			return true;
		}
		Arrays.asList(directory.listFiles()).forEach(file->{
			if (file.isDirectory()) {
				delDirectory(file);
			} else {
				file.delete();
			}
		});	
		return (directory.delete());
	}
	//
	public static boolean delDirectory(String directoryName) {
		File dir=new File(directoryName);
		return delDirectory(dir);
	}
	//
}
